import axios from 'axios';
import { getToken, log } from "./user"
// 线上地址
const proUrl = window._api;

console.log("proUrl", proUrl);

export const baseApi = process.env.NODE_ENV === 'development'
    ? '/api/'
    : proUrl + "/admin.php";

const service = axios.create({
    // process.env.NODE_ENV === 'development' 来判断是否开发环境
    baseURL: baseApi,
    timeout: 5000
});

service.interceptors.request.use(
    config => {
        const token = getToken();
        console.log('config',config.method)
        if (config.method == "get") {
            if (token) {
                config.params.apiToken = token
            }
        } else {
            if (token) {
                config.data.apiToken = token
            }
        }
        log("req", config);
        return config;
    },
    error => {
        log(error);
        return Promise.reject();
    }
);

service.interceptors.response.use(
    response => {
        if (response.status === 200) {
            return response.data;
        } else {
            Promise.reject();
        }
    },
    error => {
        log(error);
        return Promise.reject();
    }
);

export default service;
